﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NoodlesFriedFood.PlcMake
{
    public class PlcMakes
    {
        public List<object> list = new List<object>();
        public string lists = "";
        public bool readBol = true;
        private static PlcMakes instance;
        private PlcMakes() { }

        public static PlcMakes Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PlcMakes();
                }
                return instance;
            }
        }
        public void Init()
        {
            try
            {
                for (int i = 0; i < statelist.Instance.state.Count; i++)
                {
                    PlcList plc = new PlcList();
                    plc.var = statelist.Instance.state[i];
                    PlcModel model = new PlcModel();
                    model.paramss = plc;
                    model.method = "PlcProgram.Read";
                    model.id = i;
                    list.Add(model);
                }
                lists = JsonConvert.SerializeObject(list);
            }
            catch (Exception)
            {

            }
        }
        public void UpdateInit(string method)
        {
            try
            {
                List<object> Writelist = new List<object>();
                PlcListWrite plc = new PlcListWrite();
                plc.var = method;
                plc.value = 1;
                PlcModelWrite model = new PlcModelWrite();
                model.paramss = plc;
                model.method = "PlcProgram.Write";
                model.id = 1;
                Writelist.Add(model);
                var Writelists = JsonConvert.SerializeObject(Writelist);
                var readlist = HttpClient.Instance.HttpPostRaw("", Writelists);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void UpdateZHInit(string method)
        {
            try
            {
                List<object> Writelist = new List<object>();
                PlcListWrite plc = new PlcListWrite();
                plc.var = "\"Com_Data\".Command.Action2";
                plc.value = 1;
                PlcModelWrite model = new PlcModelWrite();
                model.paramss = plc;
                model.method = "PlcProgram.Write";
                model.id = 1;
                Writelist.Add(model);
                PlcListWrite plc2 = new PlcListWrite();
                plc2.var = method;
                plc2.value = 1;
                PlcModelWrite model2 = new PlcModelWrite();
                model2.paramss = plc2;
                model2.method = "PlcProgram.Write";
                model2.id = 1;
                Writelist.Add(model2);
                var Writelists = JsonConvert.SerializeObject(Writelist);
                var readlist = HttpClient.Instance.HttpPostRaw("", Writelists);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 读取数据
        /// </summary>
        public void MTRead()
        {
            try
            {
                var readlist = HttpClient.Instance.HttpPostRaw("", lists, true);
                //[["jsonrpc":"2.0","id":1,"result":0),
                if (readlist != "")
                {
                    var sss = JsonConvert.DeserializeObject<List<object>>(readlist);
                    if (sss.Count == 22)
                    {
                        for (int i = 0; i < sss.Count; i++)
                        {
                            try
                            {
                                var read = JsonConvert.DeserializeObject<PlcRead>(sss[i].ToString());
                                if (read != null)
                                {
                                    ReadBol(read.id, read.result);
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void ReadBol(int id, int result)
        {
            try
            {
                switch (id)
                {
                    case 0:
                        PlcReadMsg.Instance.Matter1 = result;
                        PlcReadBol.Instance.Matter1 = result == 0;
                        break;
                    case 1:
                        PlcReadMsg.Instance.Matter2 = result;
                        PlcReadBol.Instance.Matter2 = result == 0;
                        break;
                    case 2:
                        PlcReadMsg.Instance.Matter3 = result;
                        PlcReadBol.Instance.Matter3 = result == 0;
                        break;
                    case 3:
                        PlcReadMsg.Instance.Matter4 = result;
                        PlcReadBol.Instance.Matter4 = result == 0;
                        break;
                    case 4:
                        PlcReadMsg.Instance.Matter5 = result;
                        PlcReadBol.Instance.Matter5 = result == 0;
                        break;
                    case 5:
                        PlcReadMsg.Instance.Matter6 = result;
                        PlcReadBol.Instance.Matter6 = result == 0;
                        break;
                    case 6:
                        PlcReadMsg.Instance.Matter7 = result;
                        PlcReadBol.Instance.Matter7 = result == 0;
                        break;
                    case 7:
                        PlcReadMsg.Instance.Matter8 = result;
                        PlcReadBol.Instance.Matter8 = result == 0;
                        break;
                    case 8:
                        PlcReadMsg.Instance.Matter9 = result;
                        PlcReadBol.Instance.Matter9 = result == 0;
                        break;
                    case 9:
                        PlcReadMsg.Instance.Matter10 = result;
                        PlcReadBol.Instance.Matter10 = result == 0;
                        break;
                    case 10:
                        PlcReadMsg.Instance.Matter11 = result;
                        PlcReadBol.Instance.Matter11 = result == 0;
                        break;
                    case 11:
                        PlcReadMsg.Instance.Matter12 = result;
                        PlcReadBol.Instance.Matter12 = result == 0;
                        break;
                    case 12:
                        PlcReadMsg.Instance.Matter13 = result;
                        PlcReadBol.Instance.Matter13 = result == 0;
                        break;
                    case 13:
                        PlcReadMsg.Instance.Matter14 = result;
                        PlcReadBol.Instance.Matter14 = result == 1;
                        Console.WriteLine(PlcReadBol.Instance.Matter14);
                        break;
                    case 14:
                        PlcReadMsg.Instance.Matter15 = result;
                        PlcReadBol.Instance.Matter15 = result == 1;
                        Console.WriteLine(PlcReadMsg.Instance.Matter15 + " " + DateTime.Now.ToString());
                        break;
                    case 15:
                        PlcReadMsg.Instance.Jaka1_Error = result;
                        PlcReadBol.Instance.Jaka1_Error = result == 0;
                        break;
                    case 16:
                        PlcReadMsg.Instance.Jaka2_Error = result;
                        PlcReadBol.Instance.Jaka2_Error = result == 0;
                        break;
                    case 17:
                        PlcReadMsg.Instance.Making1_1 = result;
                        if (!PlcReadBol.Instance.Making1_1 && (result == 0))
                        {
                            EditDeviceOrderStatus sd = MainWindow.editDevicesMT.FirstOrDefault(a => a.id == 1)?.status;
                            MainWindow.editDevicesMT.Remove(MainWindow.editDevicesMT.FirstOrDefault(a => a.id == 1));
                            if (sd != null)
                            {
                                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                                status.orderGoodsId = sd.orderGoodsId;
                                status.orderId = sd.orderId;
                                status.status = "5";
                                string lists = JsonConvert.SerializeObject(status);
                                while (true)
                                {
                                    var ddd = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                                    if (ddd != null)
                                    {
                                        if (ddd != "")
                                        {
                                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd);
                                            break;
                                        }
                                    }
                                    Thread.Sleep(100);
                                }
                            }
                        }
                        PlcReadBol.Instance.Making1_1 = result == 0;
                        break;
                    case 18:
                        PlcReadMsg.Instance.Making1_2 = result;
                        if (!PlcReadBol.Instance.Making1_2 && (result == 0))
                        {
                            EditDeviceOrderStatus sd = MainWindow.editDevicesMT.FirstOrDefault(a => a.id == 2)?.status;
                            MainWindow.editDevicesMT.Remove(MainWindow.editDevicesMT.FirstOrDefault(a => a.id == 2));
                            if (sd != null)
                            {
                                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                                status.orderGoodsId = sd.orderGoodsId;
                                status.orderId = sd.orderId;
                                status.status = "5";
                                string lists = JsonConvert.SerializeObject(status);
                                while (true)
                                {
                                    var ddd = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                                    if (ddd != null)
                                    {
                                        if (ddd != "")
                                        {
                                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd);
                                            break;
                                        }
                                    }
                                    Thread.Sleep(100);
                                }
                            }
                        }
                        PlcReadBol.Instance.Making1_2 = result == 0;
                        break;
                    case 19:
                        PlcReadMsg.Instance.Making1_3 = result;
                        if (!PlcReadBol.Instance.Making1_3 && (result == 0))
                        {
                            EditDeviceOrderStatus sd = MainWindow.editDevicesMT.FirstOrDefault(a => a.id == 3)?.status;
                            MainWindow.editDevicesMT.Remove(MainWindow.editDevicesMT.FirstOrDefault(a => a.id == 3));
                            if (sd != null)
                            {
                                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                                status.orderGoodsId = sd.orderGoodsId;
                                status.orderId = sd.orderId;
                                status.status = "5";
                                string lists = JsonConvert.SerializeObject(status);
                                while (true)
                                {
                                    var ddd = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                                    if (ddd != null)
                                    {
                                        if (ddd != "")
                                        {
                                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd);
                                            break;
                                        }
                                    }
                                    Thread.Sleep(100);
                                }
                            }
                        }
                        PlcReadBol.Instance.Making1_3 = result == 0;
                        break;
                    case 20:
                        PlcReadMsg.Instance.Making2_1 = result;
                        if (!PlcReadBol.Instance.Making2_1 && (result == 0))
                        {
                            EditDeviceOrderStatus sd = MainWindow.editDevicesZH.FirstOrDefault(a => a.id == 1)?.status;
                            MainWindow.editDevicesZH.Remove(MainWindow.editDevicesZH.FirstOrDefault(a => a.id == 1));
                            if (sd != null)
                            {
                                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                                status.orderGoodsId = sd.orderGoodsId;
                                status.orderId = sd.orderId;
                                status.status = "5";
                                string lists = JsonConvert.SerializeObject(status);
                                while (true)
                                {
                                    var ddd = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                                    if (ddd != null)
                                    {
                                        if (ddd != "")
                                        {
                                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd);
                                            break;
                                        }
                                    }
                                    Thread.Sleep(100);
                                }
                            }
                           
                        }
                        PlcReadBol.Instance.Making2_1 = result == 0;
                        break;
                    case 21:
                        PlcReadMsg.Instance.Making2_2 = result;
                        if (!PlcReadBol.Instance.Making2_2 && (result == 0))
                        {
                            EditDeviceOrderStatus sd = MainWindow.editDevicesZH.FirstOrDefault(a => a.id == 2)?.status;
                            MainWindow.editDevicesZH.Remove(MainWindow.editDevicesZH.FirstOrDefault(a => a.id == 2));
                            if (sd != null)
                            {
                                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                                status.orderGoodsId = sd.orderGoodsId;
                                status.orderId = sd.orderId;
                                status.status = "5";
                                string lists = JsonConvert.SerializeObject(status);
                                while (true)
                                {
                                    var ddd = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                                    if (ddd != null)
                                    {
                                        if (ddd != "")
                                        {
                                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd);
                                            break;
                                        }
                                    }
                                    Thread.Sleep(100);
                                }
                            }
                          //  MainWindow.editDevicesZH.Remove(MainWindow.editDevicesZH.FirstOrDefault(a => a.id == 2));
                        }
                        PlcReadBol.Instance.Making2_2 = result == 0;
                        break;
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void SelRead()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (readBol)
                        {
                            MTRead();
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(500);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
